package org.artifact.xlsx;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.text.csv.CsvWriteConfig;
import cn.hutool.core.text.csv.CsvWriter;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;

import java.io.File;
import java.util.List;
import java.util.Map;

public class ServerConvert extends AbstractConvert{



    // CSV配置
    static CsvWriteConfig CSV_WRITE_CONFIG = new CsvWriteConfig();
    static {
        CSV_WRITE_CONFIG.setFieldSeparator(';');
    }

    @Override
    void convertXlsx(File file, String outputPath) {
        ExcelReader reader = ExcelUtil.getReader(file);
        reader.setIgnoreEmptyRow(true);

        String fileName = FileUtil.mainName(file);
        File outputFile = FileUtil.newFile(outputPath + File.separator + getDir() + File.separator + fileName + ".csv");

        CsvWriter writer = CsvUtil.getWriter(outputFile, CharsetUtil.CHARSET_UTF_8, false, CSV_WRITE_CONFIG);
        List<Object> types = reader.readRow(1);
        List<List<Object>> rows = reader.read();
        int row = 0;
		for (List<Object> list : rows) {
            row++;
			String[] array = new String[list.size()];
			for (int i = 0; i < list.size(); i++) {
                String content = list.get(i) == null ? "" : list.get(i).toString();
			    if (row > 4){
                    String type = types.get(i).toString();
                    switch (type){
                        case TYPE_TXT:
                            String md5 = SecureUtil.md5(content);
                            i18n.put(md5, content);
                            content = md5;
                            break;
                        default:
                            break;
                    }
                }

				array[i] = content;
			}
			writer.write(array);
		}
    }

    @Override
    void convertYml(File file, String outputPath) {
        String fileName = FileUtil.mainName(file);
        File outputFile = FileUtil.newFile(outputPath + File.separator + getDir() + File.separator + fileName + ".yml");
        FileUtil.copy(file,outputFile,true);
    }

    @Override
    String getDir() {
        return "server";
    }

    @Override
    void writeI18nFile(String outputPath) {
        File outputFile = FileUtil.newFile(outputPath + File.separator + getDir() + File.separator + "i18n.csv");
        CsvWriter writer = CsvUtil.getWriter(outputFile, CharsetUtil.CHARSET_UTF_8, false, CSV_WRITE_CONFIG);
        String[][] head = new String[][]{
                new String[]{ "id","val"},
                new String[]{ "str","str"},
                new String[]{ "主键","文字"},
                new String[]{ "c|s","c|s"}
        };
        for (String[] array : head) {
            writer.write(array);
        }


        for (Map.Entry<String, String> e : i18n.entrySet()) {
            String[] array = new String[2];
            array[0] = e.getKey();
            array[1] = e.getValue();
            writer.write(array);
        }
    }
}
